package cn.ycc1.auth.service;

import cn.ycc1.feign.clients.UserClient;
import cn.ycc1.feign.vo.UserDetailVo;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Title:
 * @Author ycc
 * @Date 2023/7/8 18:39
 */
@Service
@Data
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    UserClient userClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // User user = userClient.findByUsername(username);
//        User user = new User();
//        user.setUsername("admin");
//        user.setPassword("$2a$10$2PRhwTLo1vqHk08Oz2cHcuUdLXrS/k7uu9QzDWTYystJpdICjKHkC");
//        user.setEmail("test@qq.com");
//        return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), getAuthorities(user));

        // 将来连接数据库根据账号查询用户信息
//        UserDto userDto = userDao.getUserByUsername(username);
//        if(userDto == null){
//            //如果用户查不到，返回null，由provider来抛出异常
//            return null;
//        }
//        //根据用户的id查询用户的权限
//        List<String> permissions = userDao.findPermissionsByUserId(userDto.getId());
//        //将permissions转成数组
//        String[] permissionArray = new String[permissions.size()];
//        permissions.toArray(permissionArray);
//        User userDto;
//        userDto.setUsername("admin");
//        userDto.setPassword("$2a$10$2PRhwTLo1vqHk08Oz2cHcuUdLXrS/k7uu9QzDWTYystJpdICjKHkC");
//        userDto.setEmail("test@qq.com");


        //将userDto转成json
        // String principal = JSON.toJSONString(userDto);
        // String[] perms = {"ROLE_ADMIN","ROLE_USER"};
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        // UserDetails userDetails = User.withUsername(principal).password(userDto.getPassword()).authorities(permissionArray).build();
        // UserDetails userDetails = User.withUsername(principal).password(userDto.getPassword()).authorities(authorities).build();
//        UserDetails userDetails;
//        userDetails.setPrincipal(principal);
        // return userDetails;
        return new org.springframework.security.core.userdetails.User("admin", "$2a$10$MFqFKoDO.3odESQv3WNNO.1O9Mr.0acEnvcmVw16v9uCk3FZZqMN6", authorities);
    }

    private Collection<? extends GrantedAuthority> getAuthorities(User user) {

        // 根据用户角色返回权限列表
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        // authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        return authorities;
    }
}
